﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
                    xmlns:System="clr-namespace:System;assembly=mscorlib"
                    xmlns:Custom="clr-namespace:MahApps.Metro.Converters;assembly=MahApps.Metro">

    <Style x:Key="TextBoxStyle" TargetType="{x:Type TextBox}">
        <Setter Property="controls:TextboxHelper.IsMonitoring" Value="True"/>
        <Setter Property="SelectionBrush" Value="{DynamicResource HighlightBrush}"/>
        <Setter Property="FontFamily" Value="{DynamicResource ContentFontFamily}"/>
        <Setter Property="FontSize" Value="{DynamicResource ContentFontSize}"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="MinHeight" Value="26"/>
        <Setter Property="Background" Value="{DynamicResource ControlBackgroundBrush}"/>
        <Setter Property="Foreground" Value="{DynamicResource TextBrush}"/>
        <Setter Property="Padding" Value="0"/>
        <Setter Property="BorderBrush" Value="{DynamicResource TextBoxBorderBrush}"/>
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <ControlTemplate.Resources>
                        <Storyboard x:Key="enterGotFocus">
                            <DoubleAnimation Duration="0:0:0.2" To="0.2" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="Message"/>
                        </Storyboard>
                        <Storyboard x:Key="exitGotFocus">
                            <DoubleAnimation Duration="0:0:0.2" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="Message"/>
                        </Storyboard>
                        <Storyboard x:Key="enterHasText">
                            <DoubleAnimation Duration="0:0:0.2" From="0.2" To="0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="Message"/>
                        </Storyboard>
                        <Storyboard x:Key="exitHasText">
                            <DoubleAnimation Duration="0:0:0.2" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="Message"/>
                        </Storyboard>
                    </ControlTemplate.Resources>
                    <Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="ValidationStates">
                                <VisualState x:Name="Valid"/>
                                <VisualState x:Name="InvalidUnfocused">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="ValidationErrorElement">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Visible</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="InvalidFocused">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="ValidationErrorElement">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Visible</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsOpen" Storyboard.TargetName="validationTooltip">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <System:Boolean>True</System:Boolean>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Rectangle x:Name="Base" Fill="{TemplateBinding Background}" Opacity="1" Stroke="{TemplateBinding BorderBrush}">
                            <Rectangle.StrokeThickness>
                                <TemplateBinding Property="BorderThickness">
                                    <TemplateBinding.Converter>
                                        <Custom:ThicknessToDoubleConverter/>
                                    </TemplateBinding.Converter>
                                </TemplateBinding>
                            </Rectangle.StrokeThickness>
                        </Rectangle>
                        <Rectangle x:Name="FocusRectangle" Opacity="0" Stroke="{DynamicResource BlackBrush}">
                            <Rectangle.StrokeThickness>
                                <TemplateBinding Property="BorderThickness">
                                    <TemplateBinding.Converter>
                                        <Custom:ThicknessToDoubleConverter/>
                                    </TemplateBinding.Converter>
                                </TemplateBinding>
                            </Rectangle.StrokeThickness>
                        </Rectangle>
                        <Grid Margin="2">
                            <Border x:Name="ReadOnlyVisualElement" Background="{x:Null}" Opacity="0"/>
                            <ScrollViewer x:Name="PART_ContentHost" BorderThickness="0" Background="{x:Null}" IsTabStop="False" Margin="2" VerticalAlignment="Stretch"/>
                            <TextBlock x:Name="Message" Foreground="{TemplateBinding Foreground}" HorizontalAlignment="Left" IsHitTestVisible="False" Margin="6,0,0,0" Opacity="0.6" Text="{TemplateBinding controls:TextboxHelper.Watermark}" Visibility="Collapsed" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            <Button x:Name="PART_ClearText" Content="r" Foreground="{TemplateBinding Foreground}" FontFamily="Marlett" HorizontalAlignment="Right" IsTabStop="False" Style="{DynamicResource ChromelessButtonStyle}" Width="20">
                                <Button.Visibility>
                                    <Binding Path="(controls:TextboxHelper.ClearTextButton)" RelativeSource="{RelativeSource TemplatedParent}">
                                        <Binding.Converter>
                                            <BooleanToVisibilityConverter/>
                                        </Binding.Converter>
                                    </Binding>
                                </Button.Visibility>
                            </Button>
                        </Grid>
                        <Rectangle x:Name="DisabledVisualElement" Fill="{DynamicResource ControlsDisabledBrush}" IsHitTestVisible="False" Opacity="0" Stroke="{DynamicResource ControlsDisabledBrush}">
                            <Rectangle.StrokeThickness>
                                <TemplateBinding Property="BorderThickness">
                                    <TemplateBinding.Converter>
                                        <Custom:ThicknessToDoubleConverter/>
                                    </TemplateBinding.Converter>
                                </TemplateBinding>
                            </Rectangle.StrokeThickness>
                        </Rectangle>
                        <Border x:Name="ValidationErrorElement" BorderBrush="{DynamicResource ControlsValidationBrush}" BorderThickness="1" Visibility="Collapsed">
                            <Border.ToolTip>
                                <ToolTip x:Name="validationTooltip" Placement="Right" Template="{DynamicResource ValidationToolTipTemplate}">
                                    <ToolTip.Triggers>
                                        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
                                            <BeginStoryboard>
                                                <Storyboard>
                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsHitTestVisible">
                                                        <DiscreteObjectKeyFrame KeyTime="0">
                                                            <DiscreteObjectKeyFrame.Value>
                                                                <System:Boolean>True</System:Boolean>
                                                            </DiscreteObjectKeyFrame.Value>
                                                        </DiscreteObjectKeyFrame>
                                                    </ObjectAnimationUsingKeyFrames>
                                                </Storyboard>
                                            </BeginStoryboard>
                                        </EventTrigger>
                                    </ToolTip.Triggers>
                                </ToolTip>
                            </Border.ToolTip>
                            <Grid Background="Transparent" HorizontalAlignment="Right" Height="12" Margin="1,-4,-4,0" VerticalAlignment="Top" Width="12">
                                <Path Data="M1,0L6,0A2,2,90,0,1,8,2L8,7z" Fill="{DynamicResource ValidationBrush5}" Margin="1,3,0,0"/>
                                <Path Data="M0,0L2,0 8,6 8,8" Fill="{DynamicResource WhiteColorBrush}" Margin="1,3,0,0"/>
                            </Grid>
                        </Border>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <DataTrigger Binding="{Binding Text, RelativeSource={RelativeSource Self}}" Value="">
                            <Setter Property="Visibility" TargetName="Message" Value="Visible"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding (controls:TextboxHelper.ClearTextButton), RelativeSource={RelativeSource Self}}" Value="True">
                            <Setter Property="Margin" TargetName="PART_ContentHost" Value="2,2,20,2"/>
                        </DataTrigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Opacity" TargetName="FocusRectangle" Value="1"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Opacity" TargetName="DisabledVisualElement" Value="1"/>
                        </Trigger>
                        <Trigger Property="IsReadOnly" Value="True">
                            <Setter Property="Opacity" TargetName="ReadOnlyVisualElement" Value="1"/>
                        </Trigger>
                        <Trigger Property="IsFocused" Value="True">
                            <Setter Property="Opacity" TargetName="FocusRectangle" Value="1"/>
                        </Trigger>
                        <Trigger Property="IsMouseOver" SourceName="PART_ClearText" Value="True">
                            <Setter Property="Background" TargetName="PART_ClearText" Value="{DynamicResource GrayBrush5}"/>
                        </Trigger>
                        <Trigger Property="IsPressed" SourceName="PART_ClearText" Value="True">
                            <Setter Property="Background" TargetName="PART_ClearText" Value="{DynamicResource BlackBrush}"/>
                            <Setter Property="Foreground" TargetName="PART_ClearText" Value="{DynamicResource WhiteBrush}"/>
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="controls:TextboxHelper.HasText" Value="False"/>
                                <Condition Property="IsFocused" Value="True"/>
                            </MultiTrigger.Conditions>
                            <MultiTrigger.EnterActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation Duration="0:0:0.2" To="0.2" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="Message"/>
                                    </Storyboard>
                                </BeginStoryboard>
                            </MultiTrigger.EnterActions>
                            <MultiTrigger.ExitActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation Duration="0:0:0.2" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="Message"/>
                                    </Storyboard>
                                </BeginStoryboard>
                            </MultiTrigger.ExitActions>
                        </MultiTrigger>
                        <Trigger Property="controls:TextboxHelper.HasText" Value="True">
                            <Trigger.EnterActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation Duration="0:0:0.2" From="0.2" To="0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="Message"/>
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation Duration="0:0:0.2" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="Message"/>
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.ExitActions>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>